package zf.data.formats
{
	public class FileSizeFormat
	{
		private var total:Number;
		private var unitList:Array=["B","KB","MB","GB","TB","PB","EB","ZB","YB"];
		public function FileSizeFormat(num:Number)
		{
			total=Math.round(num);
		}
		public function toString():String
		{
			if(total==0)
			{
				return "0 B";
			}
			var t:Number=total;
			var l:uint=unitList.length;
			var i:uint=0;
			while(Math.floor(t/Math.pow(1024,i))>0 && i<l)
			{
				i++;
			}
			i--;
			var str:String=(t/Math.pow(1024,i)).toFixed(2);
			return str+" "+unitList[i];
		}
		
		/**
		 * 获得文件大小的格式化字符串 
		 * @param num 文件大小,File.size;
		 * @return 
		 * 
		 */
		public static function format(num:Number):String
		{
			var fileSize:FileSizeFormat=new FileSizeFormat(num);
			return fileSize.toString();
		}
	}
}